package com.wenjie.original.service.impl;


import com.mybatisflex.core.query.QueryWrapper;
import com.wenjie.databseDiff.entity.DatasourceConnectionTestReq;
import com.wenjie.databseDiff.enums.Drivers;
import com.wenjie.databseDiff.handle.service.DatasourceSyncService;
import com.wenjie.databseDiff.handle.service.impl.DefaultDatasourceSyncServiceImpl;
import com.wenjie.original.entity.SysDiffColumn;
import com.wenjie.original.entity.SysDiffDatasource;
import com.wenjie.original.entity.SysDiffTable;
import com.wenjie.original.entity.table.SysDiffDatasourceTableDef;
import com.wenjie.original.entity.vo.SysDiffDatasource.SysDiffDatasourceUpOrAddVo;
import com.wenjie.original.flex_config.baseInterface.Cus_ServiceImp;
import com.wenjie.original.mapper.SysDiffDatasourceMapper;
import com.wenjie.original.service.ISysDiffColumnService;
import com.wenjie.original.service.ISysDiffDatasourceService;
import com.wenjie.original.service.ISysDiffTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.wenjie.original.entity.table.SysDiffColumnTableDef.SYS_DIFF_COLUMN;
import static com.wenjie.original.entity.table.SysDiffTableTableDef.SYS_DIFF_TABLE;

/**
 * <p>
 * 叉车工 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-25
 */
@Service
public class SysDiffDatasourceServiceImpl extends Cus_ServiceImp<SysDiffDatasourceMapper, SysDiffDatasource> implements ISysDiffDatasourceService {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private ISysDiffTableService tableService;
    @Autowired
    private ISysDiffColumnService columnService;

    //
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysDiffDatasourceUpOrAddVo.AddVo addVo) {
        //数据源类型
        String type = addVo.getType();
        Drivers drivers = Drivers.of(type);
        addVo.setDriverFile(drivers.getDriver());
        addVo.setDriverClassName(drivers.getDriverClassName());
        this.save(addVo);
    }

    @Override
    public void sync(Long datasourceId) {
        DatasourceSyncService syncService = applicationContext.getBean(DefaultDatasourceSyncServiceImpl.class);
        syncService.sync(datasourceId);
    }

    //
//    @Override
//    public RestBaseVo<Datasource> list(DatasourceListReq req) {
//        List<DatasourceListRes> result = new ArrayList<>();
//        List<Datasource> list = this.list();
//        for (Datasource datasource : list) {
//            DatasourceListRes res = new DatasourceListRes();
//            BeanUtils.copyProperties(datasource, res);
//            result.add(res);
//        }
//
//
//        List<DatasourceListRes> result = new ArrayList<>();
//        PageHelper.startPage(req.getPageNum(), req.getPageSize());
//        LambdaQueryWrapper<Datasource> query = Wrappers.lambdaQuery();
//        query.eq(Datasource::getStatus, CommonStatus.OK.getCode());
//        query.orderByAsc(Datasource::getCreateDate);
//        List<Datasource> list = this.list(query);
//        for (Datasource datasource : list) {
//            DatasourceListRes res = new DatasourceListRes();
//            BeanUtils.copyProperties(datasource, res);
//            result.add(res);
//        }
//        return new PageInfo<>(result);
//    }
//
//    @Override
//    public Datasource getDatasource(Long id) {
//        Datasource datasource = this.getById(id);
//        if (datasource == null) {
//            throw new BusiException("数据源不存在");
//        }
//        return datasource;
//    }
//
    @Override
    public SysDiffDatasource getDatasourceByName(String name) {
        SysDiffDatasource datasource = this.getOne(QueryWrapper.create().where(SysDiffDatasourceTableDef.SYS_DIFF_DATASOURCE.NAME.eq(name)));
        if (datasource == null) throw new RuntimeException("数据源不存在");
        return datasource;
    }

    //
    @Override
    public void testConnection(DatasourceConnectionTestReq req) {
        DatasourceSyncService syncService = applicationContext.getBean(DefaultDatasourceSyncServiceImpl.class);
        syncService.test(req.getType(), req.getUrl(), req.getUserName(), req.getPassword());
    }

    //
//    @Override
//    public void sync(DatasourceSyncReq req) {
//        DatasourceSyncService syncService = applicationContext.getBean(DefaultDatasourceSyncServiceImpl.class);
//        syncService.sync(req.getDatasourceId());
//    }
//
    @Override
    public List<SysDiffTable> getTableInfos(Long datasourceId) {
        SysDiffDatasource datasource = this.getById(datasourceId);
        List<SysDiffTable> list = tableService.list(QueryWrapper.create().where(SYS_DIFF_TABLE.SYS_DIFF_DATASOURCE_ID.eq(datasourceId).and(SYS_DIFF_TABLE.SYS_DIFF_SYNC_ID.eq(datasource.getSysDiffSyncId()))));
        return list;
    }

    @Override
    public List<SysDiffColumn> getColumnInfos(Long datasourceId) {
        SysDiffDatasource datasource = this.getById(datasourceId);
        List<SysDiffColumn> list = columnService.list(QueryWrapper.create().where(SYS_DIFF_COLUMN.SYS_DIFF_DATASOURCE_ID.eq(datasourceId).and(SYS_DIFF_COLUMN.SYS_DIFF_SYNC_ID.eq(datasource.getSysDiffSyncId()))));
        return list;
    }
//
//    @Override
//    public void update(DatasourceUpdateReq req) {
//        this.updateById(req);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void delete(DatasourceDeleteReq req) {
//        this.removeById(req.getId());
//        LambdaQueryWrapper<SysDiffTable> tableQuery = Wrappers.lambdaQuery();
//        tableQuery.eq(SysDiffTable::getDatasourceId, req.getId());
//        tableInfoService.remove(tableQuery);
//        LambdaQueryWrapper<SysDiffColumn> columnQuery = Wrappers.lambdaQuery();
//        columnQuery.eq(SysDiffColumn::getDatasourceId, req.getId());
//        columnInfoService.remove(columnQuery);
//        LambdaQueryWrapper<History> historyQuery = Wrappers.lambdaQuery();
//        historyQuery.or((q) -> {
//            q.eq(History::getDs1Id, req.getId());
//            q.eq(History::getDs2Id, req.getId());
//        });
//        historyService.remove(historyQuery);
//    }
}
